package io.renren.modules.jxgkphone.service.Impl;


import io.renren.modules.jxgkphone.dao.ClassCourseMapper;
import io.renren.modules.jxgkphone.entity.DateInfo;
import io.renren.modules.jxgkphone.service.ClassCourseService;
import io.renren.modules.jxgkphone.tempEntity.TempClass;
import io.renren.modules.jxgkphone.tempEntity.TempDAC;
import io.renren.modules.jxgkphone.tempEntity.TempDTAC;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ClassCourseServiceImpl implements ClassCourseService {
    @Autowired
    ClassCourseMapper classCourseMapper;
    public List getDayCourse(String date, String classid){
        DateInfo di=classCourseMapper.findDateInfoByDate(date);
        TempDAC ts=new TempDAC();
        ts.setCourseAcademicTerm(di.getCourseAcademicTerm());
        ts.setCourseAcademicYear(di.getCourseAcademicYear());
        ts.setCourseWhichWeek((di.getCourseWhichWeek()));
        ts.setCourseWhichDay((di.getCourseWhichDay()));
        ts.setStudentClassName(classid);
        List<TempClass> tc=classCourseMapper.studentDayClassInfo(ts);
        for(TempClass tcc:tc){
            DateInfo dateinfo=new DateInfo();
            dateinfo.setCourseAcademicTerm(tcc.getCourseAcademicTerm());
            dateinfo.setCourseAcademicYear(tcc.getCourseAcademicYear());
            dateinfo.setCourseWhichWeek(tcc.getCourseWhichWeek());
            dateinfo.setCourseWhichDay(tcc.getCourseWhichDay());
           // String realldate=classCourseMapper.findDateByDateInfo(dateinfo);
            tcc.setCurrentDate(date);
        }
        return tc;
    }
    public List getCourseByClass(@RequestParam Map<String, Object> params) {
        return classCourseMapper.getCourseByClass(params);

    }

    @Override
    public List<Integer> getThisTerm() {
        return classCourseMapper.getThisTerm();
    }

    @Override
    public List getTeacherDayCourse(String date) {
        DateInfo di=classCourseMapper.findDateInfoByDate(date);
        TempDTAC ts=new TempDTAC();
        ts.setCourseAcademicTerm(di.getCourseAcademicTerm());
        ts.setCourseAcademicYear(di.getCourseAcademicYear());
        ts.setCourseWhichWeek((di.getCourseWhichWeek()));
        ts.setCourseWhichDay((di.getCourseWhichDay()));
        ts.setTacherId(((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUsername());
        List<TempClass> tc=classCourseMapper.TeacherDayClassInfo(ts);
        for(TempClass tcc:tc){
            DateInfo dateinfo=new DateInfo();
            dateinfo.setCourseAcademicTerm(tcc.getCourseAcademicTerm());
            dateinfo.setCourseAcademicYear(tcc.getCourseAcademicYear());
            dateinfo.setCourseWhichWeek(tcc.getCourseWhichWeek());
            dateinfo.setCourseWhichDay(tcc.getCourseWhichDay());
            String realldate=classCourseMapper.findDateByDateInfo(dateinfo);
            tcc.setCurrentDate(realldate);
        }


        return tc;
    }

    @Override
    public List getClassTeacherWeekCourse(DateInfo date) {
        TempDTAC ts=new TempDTAC();
        ts.setCourseAcademicTerm(date.getCourseAcademicTerm());
        ts.setCourseAcademicYear(date.getCourseAcademicYear());
        ts.setCourseWhichWeek((date.getCourseWhichWeek()));
        ts.setTacherId(((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUsername());
        List<TempClass> tc=classCourseMapper.teacherWeekClassInfo(ts);
        List<List<TempClass>> wl=new ArrayList<>();
        for(int i=0;i<7;i++){
            List<TempClass> daylist=new ArrayList<>();
            wl.add(daylist);
        }
        for(TempClass t:tc)
        {
            for(int j=0;j<7;j++){
                String day=(j+1)+"";
                if(t.getCourseWhichDay().equals(day)){
                    date.setCourseWhichDay(day);
                    //可能会导致性能降低
                    t.setCurrentDate(classCourseMapper.findDateByDateInfo(date));
                    wl.get(j).add(t);
                }
            }
        }
        return wl;
    }

    public List getWeekCourse(DateInfo date, String classid){
        TempDAC ts=new TempDAC();
        ts.setCourseAcademicTerm(date.getCourseAcademicTerm());
        ts.setCourseAcademicYear(date.getCourseAcademicYear());
        ts.setCourseWhichWeek((date.getCourseWhichWeek()));
        ts.setStudentClassName(classid);
        List<TempClass> tc=classCourseMapper.studentWeekClassInfo(ts);
        List<List<TempClass>> wl=new ArrayList<>();
        for(int i=0;i<7;i++){
            List<TempClass> daylist=new ArrayList<>();
            wl.add(daylist);
        }
        for(TempClass t:tc)
        {
            for(int j=0;j<7;j++){
                String day=(j+1)+"";
                if(t.getCourseWhichDay().equals(day)){
                    date.setCourseWhichDay(day);
                    //可能会导致性能降低
                    t.setCurrentDate(classCourseMapper.findDateByDateInfoList(date).get(0));
                    wl.get(j).add(t);
                }
            }
        }
        return wl;
    }
    public List getDiaryDayCourse(String classid){
        List<TempClass> tc=classCourseMapper.studentDiaryDayClassInfo(classid);
        for(TempClass tcc:tc){
            DateInfo dateinfo=new DateInfo();
            dateinfo.setCourseAcademicTerm(tcc.getCourseAcademicTerm());
            dateinfo.setCourseAcademicYear(tcc.getCourseAcademicYear());
            dateinfo.setCourseWhichWeek(tcc.getCourseWhichWeek());
            dateinfo.setCourseWhichDay(tcc.getCourseWhichDay());
            String date=classCourseMapper.findDateByDateInfo(dateinfo);
            tcc.setCurrentDate(date);
        }
        return tc;
    }
    public List getAttendanceDayCourse(String classid){
        List<TempClass> tc=classCourseMapper.studentAttendanceDayClassInfo(classid);
        for(TempClass tcc:tc){
            DateInfo dateinfo=new DateInfo();
            dateinfo.setCourseAcademicTerm(tcc.getCourseAcademicTerm());
            dateinfo.setCourseAcademicYear(tcc.getCourseAcademicYear());
            dateinfo.setCourseWhichWeek(tcc.getCourseWhichWeek());
            dateinfo.setCourseWhichDay(tcc.getCourseWhichDay());
            String realldate=classCourseMapper.findDateByDateInfo(dateinfo);
            tcc.setCurrentDate(realldate);
        }
        return tc;
    }
    public String findDateByDateInfo(DateInfo date){
        return  classCourseMapper.findDateByDateInfo(date);
    }
}
